'use strict';

var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _toConsumableArray2 = _interopRequireDefault(
  require('@babel/runtime/helpers/toConsumableArray'),
);
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _defineProperty2 = _interopRequireDefault(require('@babel/runtime/helpers/defineProperty'));
var _react = _interopRequireWildcard(require('react'));
var _PlusCircleFilled = _interopRequireDefault(require('@ant-design/icons/PlusCircleFilled'));
var _MinusCircleFilled = _interopRequireDefault(require('@ant-design/icons/MinusCircleFilled'));
var _useList2 = _interopRequireDefault(require('./useList'));
var _FormContext = require('../../contexts/FormContext');
var _FieldContext = require('../../contexts/FieldContext');
var _createField = _interopRequireDefault(require('../../createField'));
var _consts = require('../../consts');
require('./index.css');
var List = function List(_ref) {
  var value = _ref.value,
    _ref$min = _ref.min,
    min = _ref$min === void 0 ? 1 : _ref$min,
    _ref$max = _ref.max,
    max = _ref$max === void 0 ? 5 : _ref$max,
    addText = _ref.addText,
    transformFieldProps = _ref.transformFieldProps,
    children = _ref.children;
  var form = (0, _FormContext.useFormContext)();
  var _useFieldContext = (0, _FieldContext.useFieldContext)(),
    name = _useFieldContext.name;
  var _useList = (0, _useList2.default)({
      onChange: function onChange(newDataSource) {
        form.setFieldsValue((0, _defineProperty2.default)({}, name, newDataSource));
        // 在这里 getFieldsValue 数据没更新，getFieldValue 数据已更新。。。
      },

      onAddOrRemove: function onAddOrRemove() {
        form.validateFields([name]);
      },
    }),
    dataSource = _useList.dataSource,
    add = _useList.add,
    remove = _useList.remove,
    setDataSource = _useList.setDataSource,
    mainKey = _useList.mainKey;
  (0, _react.useEffect)(
    function () {
      if (value) setDataSource(value);
    },
    [setDataSource, value],
  );
  return /*#__PURE__*/ _react.default.createElement(
    'div',
    {
      className: ''.concat(_consts.ClsPrefix, '-form-list-wrap'),
    },
    /*#__PURE__*/ _react.default.createElement(
      'div',
      {
        className: ''.concat(_consts.ClsPrefix, '-form-list'),
      },
      dataSource.map(function (item, index) {
        return /*#__PURE__*/ _react.default.createElement(
          'div',
          {
            key: mainKey + index,
            className: ''.concat(_consts.ClsPrefix, '-form-list-item'),
          },
          /*#__PURE__*/ _react.default.createElement(
            'div',
            {
              className: ''.concat(_consts.ClsPrefix, '-form-list-fields'),
            },
            _react.Children.map(children, function (child) {
              return /*#__PURE__*/ (0, _react.cloneElement)(
                child,
                (0, _objectSpread2.default)(
                  (0, _objectSpread2.default)(
                    {},
                    transformFieldProps ? transformFieldProps(child.props, index) : child.props,
                  ),
                  {},
                  {
                    name: [name, index, child.props.name],
                    key: mainKey + index + child.props.name,
                    className: ''.concat(_consts.ClsPrefix, '-form-list-field'),
                    onChange: function onChange(event) {
                      if (event && event.target) {
                        dataSource[index][child.props.name] = child.props.getValueFromEvent
                          ? child.props.getValueFromEvent(event.target.value)
                          : event.target.value || event.target.checked;
                      } else {
                        dataSource[index][child.props.name] = event;
                      }
                      setDataSource((0, _toConsumableArray2.default)(dataSource));
                    },
                  },
                ),
              );
            }),
          ),
          index < min
            ? null
            : /*#__PURE__*/ _react.default.createElement(
                'span',
                {
                  className: ''.concat(_consts.ClsPrefix, '-form-list-remove'),
                  onClick: function onClick() {
                    remove(index);
                  },
                },
                /*#__PURE__*/ _react.default.createElement(_MinusCircleFilled.default, {
                  style: {
                    marginRight: '6px',
                    fontSize: '16px',
                    verticalAlign: 'sub',
                  },
                }),
                '\u5220\u9664',
              ),
        );
      }),
    ),
    /*#__PURE__*/ _react.default.createElement(
      'div',
      {
        className:
          dataSource.length == max
            ? ''.concat(_consts.ClsPrefix, '-form-list-add disabled')
            : ''.concat(_consts.ClsPrefix, '-form-list-add'),
        onClick: function onClick() {
          if (dataSource.length < max) add();
        },
      },
      /*#__PURE__*/ _react.default.createElement(_PlusCircleFilled.default, {
        style: {
          marginRight: '6px',
          fontSize: '16px',
          verticalAlign: 'sub',
        },
      }),
      addText ? addText : '添加',
    ),
  );
};
var _default = (0, _createField.default)(List, {
  defaultDetailType: 'list',
  defaultFormItemProps: {
    shouldUpdate: true,
  },
});
exports.default = _default;
